﻿using KinonekoSoftware.CodariaDB.Runtimes;
using KinonekoSoftware.Extensions.Reflections;
using KinonekoSoftware.Foundation.UI;

namespace KinonekoSoftware.Studio.Services.DataParts
{
    public static class ComponentViewFactory
    {
        private static readonly Dictionary<Type, Factory<FrameworkElement>> _FeatureContentFactoryMap;

        static ComponentViewFactory()
        {
            _FeatureContentFactoryMap = new Dictionary<Type, Factory<FrameworkElement>>();

            //
            //
            Initialize();
        }

        /*
         * Regex : ([\w\d]+)
         * Replace Regex: _FeatureContentFactoryMap.Add(typeof($1EditorFeature), CreateView<$1View>);
         *
         * Input: InstanceBasic
         * Output: _FeatureContentFactoryMap.Add(typeof(InstanceBasicEditorFeature), CreateView<InstanceBasicView>);
         *
         *
         * Test:
         *
         *  _FeatureContentFactoryMap.Add(typeof(GeographyBasicEditorFeature), CreateView<GeographyBasicView>);
         *  _FeatureContentFactoryMap.Add(typeof(GeographyDetailEditorFeature), CreateView<GeographyDetailView>);
         *  _FeatureContentFactoryMap.Add(typeof(ClassesEditorFeature), CreateView<InstanceClassesView>);
         *  _FeatureContentFactoryMap.Add(typeof(BiologicalBasicEditorFeature), CreateView<BiologicalBasicView>);
         *  _FeatureContentFactoryMap.Add(typeof(BiologicalDetailEditorFeature), CreateView<BiologicalDetailView>);
         */

        static void Initialize()
        {
        }

        private static FrameworkElement CreateView<T>() where T : FrameworkElement => ClassStatic.CreateInstance<T>();

        public static FrameworkElement GetView(Type type)
        {
            if (_FeatureContentFactoryMap.TryGetValue(type, out var factory) && factory is not null)
            {
                return factory();
            }
            
            Xplat.Debug($"缺失DataPartComponent的UI映射，{type.Name}");
            return null;
        }

    }
}